﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using blackhouse.NemoBlog.Business.View;
using blackhouse.WebNemo.Business.Users;
using System.Configuration;
using blackhouse.NemoBlog.Business.Model;

namespace blackhouse.NemoBlog.Business.Presenter {
    public class PostListPresenter {

        #region Fields

        private IPostListView view;
        private NemoBlogSettings settings;
        private INemoUser user = blackhouse.WebNemo.Business.Factory.Instance().GetCurrentUser();
        private NemoBlogPost[] postsToLoad;
        private NemoBlogTag selectedTag;
        private IPostListModel model;

        #endregion

        #region Constructors

        public PostListPresenter(IPostListView view, IPostListModel model) {
            this.view = view;
            blackhouse.NemoBlog.Business.Install.NemoBlogInstaller.CheckInstall();
            this.model = model;
            if (this.model == null)
                this.model = new StandardPostListModel();
            this.model.PlaceId = this.view.PlaceId;
        }

        #endregion

        #region Methods

        public void Load() {
            int postCount = this.model.PostCountOnPage;
            if (this.view.RequireSnapshoot)
                postCount = 100000;
            this.Load(postCount);
        }

        public void Load(int postCount) {
            this.SetBlogSettings();
            this.PopulateBlogHeader();
            this.view.IsPublisher = this.user.HasSufficientPermissions(false);
            if (this.view.IsPublisher)
                this.PopulateActionPanelLinks();
            if (this.view.SelectedTagId > 0) {
                this.selectedTag = new NemoBlogTag(this.view.SelectedTagId);
                if (!this.selectedTag.Exists)
                    this.selectedTag = null;
            }
            if (this.selectedTag == null) {
                if (this.view.LastPostId > 0)
                    this.postsToLoad = this.model.GetLastPosts(this.view.LastPostId, postCount, !this.view.IsPublisher);
                else
                    this.postsToLoad = this.model.GetLastPosts(postCount, !this.view.IsPublisher);
            }
            else {
                this.postsToLoad = this.model.GetPostsByTag(this.selectedTag, postCount, !this.view.IsPublisher);
                this.view.BlogTitle = this.selectedTag.Title;
                this.view.BlogSubtitleVisible = false;
                this.view.SetSeo(this.selectedTag.Title, this.selectedTag.TagUrl);
            }
            this.view.SeeMoreButtonVisible = this.postsToLoad.Length == postCount;
            if (postsToLoad.Length == 0) {
                this.view.PostsVisible = false;
                this.view.NoPostsVisible = true;
                if (this.view.IsPublisher)
                    this.view.SetAddNewLinkInNoPostContent(NemoBlogPosts.BelongToGroup(), this.view.PageId, this.view.PlaceId);
            }
            else {
                this.view.PopulatePosts(postsToLoad);
                this.view.PostsVisible = true;
                this.view.NoPostsVisible = false;
            }
        }

        private void SetBlogSettings() {
            this.settings = new NemoBlogSettings(this.view.PlaceId);
            if (!settings.Exists) {
                settings.BlogTitle = this.view.SuggestDefaultTitle();
                settings.Save();
            }
        }

        public void PopulateBlogHeader() {
            this.view.BlogTitle = settings.BlogTitle;
            this.view.BlogSubtitleVisible = !string.IsNullOrEmpty(settings.BlogSubtitle);
            this.view.BlogSubtitle = settings.BlogSubtitle;
        }

        private void PopulateActionPanelLinks() {
            this.view.SetSettingsLink(this.settings.GetGroup(), this.settings.RecordId);
            this.view.SetAddNewLink(NemoBlogPosts.BelongToGroup(), this.view.PageId, this.view.PlaceId);
        }

        #endregion

    }
}
